package net.huashitong.supervision.insideDuCha.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.insideDuCha.dao.InsideSubTaskDao;
import net.huashitong.supervision.insideDuCha.entity.InsideMainTaskEntity;
import net.huashitong.supervision.insideDuCha.entity.InsideSubTaskEntity;
import net.huashitong.supervision.insideDuCha.filter.InsideSubTaskFilter;
import net.huashitong.supervision.insideDuCha.filter.InsideUserTaskFilter;
import net.huashitong.supervision.insideDuCha.util.CommUtil;
import net.huashitong.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class InsideSubTaskService extends StringPKService<InsideSubTaskEntity> {

    @Autowired
    private InsideSubTaskDao dao;
    @Autowired
    private SysTableService sysTableService;
    @Autowired
    private InsideUserTaskService userTaskService;

    @Override
    protected BaseDao<InsideSubTaskEntity, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(InsideSubTaskEntity obj) {
        obj.setSysId(sysTableService.updateMaxSysId("inside_task_sub",null));
    }

    public  void save(InsideSubTaskEntity entity,String userId)
    {
        beforeSave(entity);
        entity.setCreateDate(DateUtil.getCurrentDate());
        entity.setCreateTime(DateUtil.getCurrentTime());
        entity.setCreateUser(userId);
        dao.save(entity);
    }


    /**
     * 处理保存子任务
     * @param
     * @param lists
     * @return
     */
    public List<InsideSubTaskFilter> saveSubTasks(InsideMainTaskEntity mainTask, List<InsideSubTaskFilter> lists){
        for (InsideSubTaskFilter item : lists) {
            InsideSubTaskEntity entity = new InsideSubTaskEntity();
            String deptId = mainTask.getDeptId();
            //如果子任务时限为空，默认主任务的时限
            String[] strings=null;
            if(item.getTimeType()==1&& StringUtils.isBlank(item.getSubTimeLimit())){
                entity.setTimeType(mainTask.getTimeType());
                entity.setCycleValue(mainTask.getCycleValue());
            }else {

                //处理时限；
                strings = CommUtil.changeTimeType(item.getTimeType(), item.getPeriod(),
                        item.getDay(), item.getWeek(), item.getMonth(),
                        item.getQuarter(), item.getSubTimeLimit(), item.getTimeLimits());
                if("0".equals(strings[0])){
                    return  null;
                }
                entity.setTimeType(Integer.parseInt(strings[0]));
                entity.setCycleValue(strings[1]);
            }


            entity.setRoomId(item.getRoomId());
            entity.setDeptId(deptId);
            entity.setSysId(null);
            String remindDate = CommUtil.remindDate(entity.getTimeType(), entity.getCycleValue());

            entity.setRemindDate(remindDate);
            entity.setTimeRound(1);
            entity.setValids(1);
            entity.setSubTaskName(item.getSubTaskName());
            entity.setTaskMainId(mainTask.getSysId());
            entity.setTimeAging("1");
            entity.setTaskState("0");
            entity.setBackType("0");

            System.out.println("子任务循环值："+entity.getCycleValue());
            System.out.println("时间周期性："+entity.getTimeType());
            if(StringUtils.isBlank(SysUserUtils.getUserId()))
            {
                save(entity,mainTask.getCreateUser());
            }else {
                this.save(entity);

            }
            item.setRemindDate(remindDate);
            item.setTimeRound(entity.getTimeRound());
            item.setTimeType(entity.getTimeType());
            item.setCycleValue(entity.getCycleValue());
            item.setSysId(entity.getSysId());
            item.setTaskMainId(mainTask.getSysId());
            item.setCreateUser(mainTask.getCreateUser());
            item.setDeptId(mainTask.getDeptId());
        }

        return  lists;
    }


    //查看子任务
    public List<InsideSubTaskEntity> getListByMainId(String taskId,String roleId ){

       // String roleId = SysUserUtils.getRoleId();
        InsideSubTaskFilter filter = new InsideSubTaskFilter();

        filter.setTaskMainId(taskId);
        List<InsideSubTaskEntity> listSubLists = dao.getListSubLists(filter);

       List<InsideSubTaskEntity> subLists = new ArrayList<>();

        for (InsideSubTaskEntity item : listSubLists) {
            InsideUserTaskFilter usfilter = new InsideUserTaskFilter();
            usfilter.setSubTaskId(item.getSysId());
            List<InsideUserTaskFilter> userlist = userTaskService.getUserlist(usfilter);
            if(ObjectUtils.isNotBlank(userlist)){
                for (InsideUserTaskFilter itemuser : userlist) {
                    InsideSubTaskEntity entity =new InsideSubTaskEntity();
                    BeanUtils.copyProperties(item,entity);
                    entity.setUserId(itemuser.getUserId());
                    entity.setUserName(itemuser.getUserName());
                    entity.setUserTaskId(itemuser.getSysId());
                    entity.setRoomId(itemuser.getRoomId());
                    entity.setRoomName(itemuser.getRoomName());
                    entity.setBackType(itemuser.getBackType());
                    subLists.add(entity);
                }

            }

        }
        if(!"02".equals(roleId)){

            subLists = subLists.stream().filter(insideSubTaskEntity -> insideSubTaskEntity.getUserId().equals(SysUserUtils.getUserId())).collect(Collectors.toList());
        }

        return subLists;
    }

    public List<InsideSubTaskEntity> getByMainIdNoUserTask(String taskId){
        InsideSubTaskFilter filter = new InsideSubTaskFilter();

        filter.setTaskMainId(taskId);
        List<InsideSubTaskEntity> listSubLists = dao.getListSubLists(filter);

        return  listSubLists;

    }

}
